package com.personal.common.util;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

/**
 * 常量定义
 * 
 * @author yugc
 * 
 */
@Repository
public final class Constants {
	private static Logger log = LoggerFactory.getLogger(Constants.class);
	
	/** 超级管理员ID */
	public static final int SUPER_ADMIN = 1;
	
	public static final String BACKSLASH = "/";
	public static final String SLASH = "\\";
	public static final String LF = "\n";
	public static final String CR = "\r";
	public static final String COMMA = ",";
	public static final String EQUAL = "=";
	public static final String SEMICOLON = ";";
	public static final String COLON = ":";
	public static final String DOT = ".";
	public static final String BAR = "|";
	public static final String UNDERLINE = "_";
	public static final String AT = "@";
	
	public static final String FILE_SEPARATOR = "/";// System.getProperty("file.separator");
	public static final String COMMA_SPLITTER = ",";
	public static Configuration VERSION_PROPERTIES = new Configuration("/version.properties");
	public static Configuration DEPLOY_PROPERTIES = new Configuration("/deploy.properties");
	private static String version = VERSION_PROPERTIES.getValue("version");

	public static final String ENCODING_UTF8 = "UTF-8";
	
	//token key
	public final static String TOKEN_KEY  = "tokenval";
	public static final String LOGIN_ACCOUNT_ID = "loginAccountId";      //user主键id
	public static final String LOGIN_ACCOUNT_ACCOUNT = "loginAccount";     //user的userid(登陆用户名)
	public static final String LOGIN_ACCOUNT_ROLEID = "roleId";         //角色ID
	public static final String LOGIN_USER = "currLoginUser";         //user
	
	/**
	 * 获得请求路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getRequestPath(HttpServletRequest request) {

//		String requestPath = request.getRequestURI() + "?" + request.getQueryString();
		String queryString = request.getQueryString();
		String requestPath = request.getRequestURI();
		if(StringUtils.isNotEmpty(queryString)){
			requestPath += "?" + queryString;
		}

		if (requestPath.indexOf("&") > -1) {// 去掉其他参数
			requestPath = requestPath.substring(0, requestPath.indexOf("&"));
		}
		requestPath = requestPath.substring(request.getContextPath().length() + 1);// 去掉项目路径
		return requestPath;
	}
	
	@PostConstruct
	private void reload(){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while (true) {
					try {
						if (!VERSION_PROPERTIES.getValue("version").equals(version)) {
							DEPLOY_PROPERTIES = new Configuration("/deploy.properties");
							version = VERSION_PROPERTIES.getValue("version");
							log.info("配置文件刷新");
						} else {
							Thread.sleep(60000);
							VERSION_PROPERTIES = new Configuration("/version.properties");
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
	
}
